package com.olocat.cjx.nsims.service.impl

import com.olocat.cjx.nsims.dao.PatientRepository
import com.olocat.cjx.nsims.dao.WardRepository
import com.olocat.cjx.nsims.pojo.info.Bed
import com.olocat.cjx.nsims.pojo.personnel.Patient
import com.olocat.cjx.nsims.pojo.personnel.Ward
import com.olocat.cjx.nsims.service.WardService
import org.springframework.stereotype.Service
import javax.annotation.Resource

@Service
class WardServiceImpl : WardService {

    @Resource
    lateinit var wardRepository: WardRepository
    @Resource
    lateinit var patientRepository: PatientRepository

    override fun list(): List<Ward> {
        return wardRepository.findAll()
    }

    override fun getById(id: String): Ward {
        return wardRepository.getOne(id)
    }

    override fun updateById(ward: Ward) {
        wardRepository.save(ward)
    }

    override fun listBedPatient(wardNumber:String): List<Bed> {
        val patients = patientRepository.getByWard(wardNumber)
        val ward = wardRepository.getWardsByNumber(wardNumber)
        println(ward)
        val beds = ArrayList<Bed>(ward.count!!)
        for (i in 1 until ward.count!! + 1) {
            beds.add(Bed(i, existNumberInPatients(i, patients)))
        }
        return beds
    }

    fun existNumberInPatients(bed:Int, patients: List<Patient>) : String? {
        for (p in patients) {
            if(p.bed == bed) return p.name
        }
        return null
    }

    override fun save(ward: Ward) {
        wardRepository.save(ward)
    }

    override fun removeByNumber(number: String) {
        wardRepository.deleteById(number)
    }

    override fun getWardsByDepartment(department: Int):List<Ward> {
        return wardRepository.getWardsByDepartment(department)
    }

    override fun getWardsByNurse(nurse: String): List<Ward> {
        return wardRepository.getWardsByNurse(nurse)
    }

    override fun getWardsByDoctor(doctor: String): List<Ward> {
        return wardRepository.getWardsByDoctor(doctor)
    }
}